// miniprogram/pages/admin/subscribeMsg/subscribeMsg.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainSwitch: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   


  },
  openSetting() {
    wx.openSetting({
      success(res) {
        console.log(res.authSetting)

      }
    })
  },
  requestSubscribeMessage() {
    // const templates = this.data.templates
    const templateId = ['fgN7PR9idyvoDi2-l0JW-ObwnAY5cRIfz1oA002FipU', 'uzadlWcZzW4JC3rVj-vHpuVrkcR-aQ38vrpspE5gNDY', 'd2Fy2BPP1uRDHpvrsazOqDV3g4POTR9_kcOmGgYTT10']
    wx.requestSubscribeMessage({
      tmplIds: templateId,
      success: (res2) => {
        console.log(res2)
        this.onShow()
      },
      fail: (err) => {
        console.log('msg', `失败（${JSON.stringify(err)}）`)

      },
      complete: (res) => {

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    const templates = [{
      id: 'fgN7PR9idyvoDi2-l0JW-ObwnAY5cRIfz1oA002FipU',
      title:'新订单通知'
    }, {
      id: 'uzadlWcZzW4JC3rVj-vHpuVrkcR-aQ38vrpspE5gNDY',
      title:'拍摄日程提醒'
    }, {
      id: 'd2Fy2BPP1uRDHpvrsazOqDV3g4POTR9_kcOmGgYTT10',
      title:'客户选片结束通知'
    }    , {
      id: '6Yl15FvAIoRqTCjWYrf7vH1NcnPRzRFNBFiosr-BxyU',
      title:'新用户加入通知'
    }, {
      id: '4aMenw5kqZCNYGZA3IvAIRrwun7oMIRPaPWx89IIuCs',
      title:'用户加入审核成功通知'
    }
  ]
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
       console.log(res.subscriptionsSetting)
        let mainSwitch = res.subscriptionsSetting.mainSwitch
        let itemSettings = res.subscriptionsSetting.itemSettings
        let rejectSum = 0
        if (itemSettings) {
           templates.map(function(item){
            item.status = itemSettings[item.id]
           })
           rejectSum =  templates.filter(item=>item.status==="reject").length
           if(rejectSum==0)
           rejectSum = -1
        // console.log(templates)
        }else{
          rejectSum = 0
        }
        console.log(templates,rejectSum,mainSwitch)
        that.setData({
          templates,
          rejectSum,
          mainSwitch

        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})